package com.wanjia.estate.service.bdservice.sjdspireservice;

import com.spire.xls.ExcelPicture;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.wanjia.estate.pojo.entity.Sjd;
import com.wanjia.estate.utils.BarcodeGeneratorUtil;
import com.wanjia.estate.utils.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

//Excel表格添加图片
@Slf4j
@Service
public class ExcelAddImgSjdService {
    public String ExcelAddImg(String excelPath, Sjd sjd) throws IOException {

        File file = new File(excelPath);
        String absolutePath = file.getAbsolutePath();
        String name = absolutePath.split("\\.")[0];
        //BarCodeUtil.getBarCode(sjd.getSjbh(),name+sjd.getSjbh()+".png");//生成条形码
        BarcodeGeneratorUtil.getBarCode(sjd.getSjbh(),name+sjd.getSjbh()+".png");
        Base64Util.stringToImage(sjd.getSqrqm(), name+"qm1");//生成签名

        //创建Workbook实例
        Workbook workbook = new Workbook();
        //加载Excel文档
        workbook.loadFromFile(excelPath);
        //获取第一张工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //添加图片到工作表的指定位置
        ExcelPicture pic1 = sheet.getPictures().add(2, 6,name+sjd.getSjbh()+".png");
        ExcelPicture pic2 = sheet.getPictures().add(24, 3,name+"qm1.png");


        //设置图片的宽度和高度
        pic1.setWidth(200);
        pic1.setHeight(40);
        pic2.setWidth(50);
        pic2.setHeight(30);
        //保存文档
        workbook.saveToFile(excelPath, ExcelVersion.Version2013);

        return absolutePath;
    }
}
